<?php
namespace service\services;

use service\entity\User;
use service\org\util\TpString;
use  think\Db;
use think\Log;
use  service\algo\AlgoLogic;

use  service\services\QuestionService;


class ZhlxService extends CommonService
{

    public function __construct()
    {

    }

    /**
     * 获取综合练习的知识饼图的消息.
     */
    public function getUserZhlxTagReport($topicId)
    {
        $user_id = $this->getUserId();


        $xiance_module_type = config("xiance_module_type");
        $bxbl_module_type = config("bxbl_module_type");
        $zhlx_module_type = config('zonghe_module_type');
        $kmap_code = config('kmap_code');  //知识图谱
        $algoLogic = new AlgoLogic();
        $question_service = new QuestionService();
        $all_knowledgeList = $question_service->getKnowledgeList();
        $knowledgeList = $all_knowledgeList[$kmap_code];

        $weakElements = $algoLogic->getWeakElements("",$topicId);
        $total_knowledge_num = count($knowledgeList);  //总知识点数量.
        $weakElements_num =  count($weakElements); //薄弱知识点数量.


        $has_learned_num = $total_knowledge_num-$weakElements_num;  //已学会知识点
//        $sql = "select tag_code from ct_user_exam_detail where user_id=$user_id and topicId=$topicId  and module_type=$module_type and (is_right=0 or is_view_answer = 1 )  and tag_code in ('" . $tag_code_str . "')";

        $sql = "SELECT tag_code,ability FROM `ct_user_ability`  where user_id =$user_id  and module_type!=1  and ability >=0.8";


        $return_ability = Db::query($sql);
        $learned_weak_element = array();
        foreach ($return_ability as $key =>$val)
        {
            $learned_weak_element[] = $val['tag_code'];
        }


        foreach ($knowledgeList as $k => $v) {
            $all_tag_code[] = $v['code'];
        }
        if($weakElements==false)$weakElements=[];
        $has_learned_tag_code  = array_diff($all_tag_code, $weakElements);
        $learned_weak_element = array_intersect($weakElements,$learned_weak_element);
        $zhlx_not_leared_tag_code  = array_diff($weakElements, $learned_weak_element);


        $has_learned_tag_code_info  = $this->getTagCodeInfo($has_learned_tag_code);

        $learned_weak_tag_info = $this->getTagCodeInfo($learned_weak_element);

        $zhlx_not_leared_tag_code_info =  $this->getTagCodeInfo($zhlx_not_leared_tag_code);



       
        $learned_weak_element_num  = count($learned_weak_element);
        $zhlx_not_leared_num = $weakElements_num - $learned_weak_element_num;
        $return_data = array(
            array(
                'xiance_has_learned_num'=>$has_learned_num,
                'xiance_tag_arr_info'=> $has_learned_tag_code_info
            ),
            array(
                'zhlx_has_leared_num'=>$learned_weak_element_num,
                'zhlx_tag_arr_info'=>$learned_weak_tag_info
            ),
            array(
                'zhlx_not_leared_num'=>$zhlx_not_leared_num,
                'zhlx_not_leared_tag_code_info'=>$zhlx_not_leared_tag_code_info
            )
        );

        return $return_data;
    }


    public function getTagCodeInfo($tag_code_arr = array())
    {
        $kmap_code = config('kmap_code');  //知识图谱       
        $question_service = new QuestionService();
        $all_knowledgeList = $question_service->getKnowledgeList();
        $knowledgeList = $all_knowledgeList[$kmap_code];
        $return_array = array();
        foreach ($knowledgeList as $k => $v) {
            foreach ($tag_code_arr as $kk => $vv) {
                if($vv == $v['code'])
                {
                    $return_array[] = $v;
                }
            }
        }
        return  $return_array;

    }


    /**
     * 获取综合练习的平均能力值.
     */
    public function  getZhlxAverageNum($topicId)
    {
        $user_id =$this->getUserId();
        $where['user_id']  = $user_id;
        $where['module_type'] =config("zhlx_module_type");
        $where['topicId'] = $topicId;
        $return_data = Db::name('user_ability')->field('ability')->where($where)->select();
        $num = count($return_data);
        $total_ability = 0;
        foreach ($return_data   as  $key=>$val) {
            $total_ability += $val['ability'];
        }
        if($total_ability==0)return 0;
        $averageNum = $total_ability/$num;
        return $averageNum;
    }



}
